/****************************************************************************
**
** Copyright (C) 2011 Jesús Martínez Pérez
**
** This program is free software: you can redistribute it and/or modify it
** under the terms of the GNU General Public License as published by the 
** Free Software Foundation, either version 3 of the License, or (at your
** option) any later version.
**
** This program is distributed in the hope that it will be useful, but 
** WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
** or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
** for more details.
**
** You should have received a copy of the GNU General Public License along 
** with this program. If not, see <http://www.gnu.org/licenses/>
**
****************************************************************************/
#include <vd_unsignedchar.h>


VD_unsignedchar::VD_unsignedchar(){
	r=MAX;
	n=0;
	v=new unsigned char[MAX];
}


VD_unsignedchar::VD_unsignedchar(int tam){
	r=tam;
	n=0;
	v=new unsigned char[tam];
}


VD_unsignedchar::VD_unsignedchar(const VD_unsignedchar &V){
	r=V.r;
	n=V.n;
	v=new unsigned char[r];
	for(int i=0;i<n;i++)
		v[i]=V.v[i];
}


void VD_unsignedchar::Ampliar(){
	r*=2;
	unsigned char *aux=new unsigned char [r];
	for(int i=0;i<n;i++){
		aux[i]=v[i];
	}
	delete [] v;
	v=aux;
}


VD_unsignedchar::~VD_unsignedchar(){

}


VD_unsignedchar &VD_unsignedchar::operator=(const VD_unsignedchar &V){
	r=V.r;
	n=V.n;
	v=new unsigned char[r];
	for(int i=0;i<n;i++){
		v[i]=V.v[i];
	}
	return *this;
}


int VD_unsignedchar::Size() const {
	return n;
}


void VD_unsignedchar::Add(unsigned char a){
	if((n>=r)){
		Ampliar();
	}
	v[n]=a;
	n++;
}


unsigned char VD_unsignedchar::Get(int i)const{
	return v[i];
}


void VD_unsignedchar::Set(int i, unsigned char valor){
	if((i>=0)&&(i<=n-1))
		this->v[i]=valor;
}


void VD_unsignedchar::Borrar(int pos){
	for(int i=pos+1;i<n;i++)
		v[i-1]=v[i];
	n--;
}


bool VD_unsignedchar::operator==( VD_unsignedchar &vd){
	if(n==vd.n){
		for(int i=0;i<n;i++)
			if(v[i]!=vd.v[i])
				return false;
			return true;
	}else
		return false;
}


bool VD_unsignedchar::operator!=( VD_unsignedchar &vd){
	return !(*this==vd);
}


VD_unsignedchar VD_unsignedchar::operator+(unsigned char a){
	VD_unsignedchar Vaux(*this);
	Vaux.Add(a);
	return Vaux;
}


VD_unsignedchar VD_unsignedchar::operator+(VD_unsignedchar &v){
	VD_unsignedchar Vaux(*this);
	for(int i=0; i<v.Size();i++){
		unsigned char a=v.Get(i);
		Vaux.Add(a);
	}
	return Vaux;
}


unsigned char &VD_unsignedchar::operator[](int i){
	return v[i];	
}


VD_unsignedchar operator+(unsigned char a,const VD_unsignedchar &vd){
	VD_unsignedchar Vaux(vd.n+1);
	Vaux.v[0]=a;
	for(int i=0;i<vd.n;i++){
		Vaux.v[i+1]=vd.v[i];
	}
	Vaux.n=vd.n+1;
	return Vaux;
}


ostream & operator<<(ostream &os,const VD_unsignedchar &vd){
	os<<vd.n<<"->";
	for(int i=0;i<vd.n;i++){
		os<<(int)vd.v[i];
	}
	return os;
}


istream & operator>>(istream &is,VD_unsignedchar &vd){
	int n;
	is>>n;
	VD_unsignedchar Vaux(1);
	char l[n];
	for(int i=0;i<n;i++){
		l[i]=cin.get();
		Vaux.Add(l[i]);
	}
	is.get(l,n);
	vd=Vaux;
	return is;
}
